/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */

package edu.bupt.ooad.library.ui;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFormattedTextField;

/**
 * @author Hamed Abdollahpour
 */
public class AddStudentDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();

    private JTextField firstnameTextField;

    private JTextField lastNameTextField;

    private JFormattedTextField idTextField;

    public String firstname;

    public String lastname;

    public int studentId;

    /**
     * Create the dialog.
     */
    public AddStudentDialog(Dialog parent) {
        super(parent, true);

        setTitle("Add student");
        setBounds(100, 100, 448, 180);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new MigLayout("", "[][grow]", "[][][]"));
        {
            JLabel label1 = new JLabel("First name:");
            contentPanel.add(label1, "cell 0 0,alignx trailing");
        }
        {
            firstnameTextField = new JTextField();
            contentPanel.add(firstnameTextField, "cell 1 0,growx");
            firstnameTextField.setColumns(10);
        }
        {
            JLabel label2 = new JLabel("Last name:");
            contentPanel.add(label2, "cell 0 1,alignx trailing");
        }
        {
            lastNameTextField = new JTextField();
            contentPanel.add(lastNameTextField, "cell 1 1,growx");
            lastNameTextField.setColumns(10);
        }
        {
            JLabel label3 = new JLabel("Student ID:");
            contentPanel.add(label3, "cell 0 2,alignx trailing");
        }
        {
            idTextField = new JFormattedTextField();
            contentPanel.add(idTextField, "cell 1 2,growx");
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg) {
                        add();
                    }
                });
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg) {
                        setVisible(false);
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

    private void add() {
        firstname = firstnameTextField.getText();
        lastname = lastNameTextField.getText();
        studentId = 0;

        if (firstname == null || firstname.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid firstname");
            return;
        }

        if (lastname == null || lastname.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid lastname");
            return;
        }

        try {
            String idString = idTextField.getText();
            studentId = Integer.parseInt(idString);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Please student ID");
            return;
        }

        setVisible(false);
    }

}
